home *** CD-ROM | disk | FTP | other *** search
- /* fgetpos.c --- p 415 */
- #include <stdio.h>
- void read10char(FILE *, char *);
- main()
- {
- fpos_t curpos;
- FILE *infile;
- char file3[81], buffer[40];
- printf ("Enter name of a text file: ");
- gets(file3);
- /* Open the file for reading */
- if ((infile = fopen(file3, "r")) == NULL)
- {
- printf("fopen failed.\n");
- exit(0);
- }
- read10char(infile, buffer);
- /* Save current position */
- if (fgetpos(infile, &curpos) != 0)
- perror("fgetpos failed!");
- /* Read another 10 characters */
- read10char(infile, &buffer[11]);
- /* Reset to previous position in file */
- if (fsetpos(infile, &curpos) != 0)
- perror("fsetpos failed!");
- /* Read another 10 characters -- these should be same
- * as last 10. */
- read10char(infile, &buffer[21]);
- buffer[32] = '\0'; /* Convert to C string */
- printf ("Buffer now has:\n%s", buffer);
- }
- /******************************/
- void read10char(FILE *infile, char *buffer)
- {
- int i;
- for (i=0; i<10; i++)
- {
- if ((*buffer = fgetc(infile)) == EOF)
- {
- printf("file ended. buffer so far has: \%s\n",
- buffer);
- exit(0);
- }
- buffer++;
- }
- *buffer = '\n';
- }